home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / tlslite / integration / TLSAsyncDispatcherMixIn.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.4 KB  |  149 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''TLS Lite + asyncore.'''
  5. import asyncore
  6. from gdata.tlslite.TLSConnection import TLSConnection
  7. from AsyncStateMachine import AsyncStateMachine
  8.  
  9. class TLSAsyncDispatcherMixIn(AsyncStateMachine):
  10.     '''This class can be "mixed in" with an
  11.     L{asyncore.dispatcher} to add TLS support.
  12.  
  13.     This class essentially sits between the dispatcher and the select
  14.     loop, intercepting events and only calling the dispatcher when
  15.     applicable.
  16.  
  17.     In the case of handle_read(), a read operation will be activated,
  18.     and when it completes, the bytes will be placed in a buffer where
  19.     the dispatcher can retrieve them by calling recv(), and the
  20.     dispatcher\'s handle_read() will be called.
  21.  
  22.     In the case of handle_write(), the dispatcher\'s handle_write() will
  23.     be called, and when it calls send(), a write operation will be
  24.     activated.
  25.  
  26.     To use this class, you must combine it with an asyncore.dispatcher,
  27.     and pass in a handshake operation with setServerHandshakeOp().
  28.  
  29.     Below is an example of using this class with medusa.  This class is
  30.     mixed in with http_channel to create http_tls_channel.  Note:
  31.      1. the mix-in is listed first in the inheritance list
  32.  
  33.      2. the input buffer size must be at least 16K, otherwise the
  34.        dispatcher might not read all the bytes from the TLS layer,
  35.        leaving some bytes in limbo.
  36.  
  37.      3. IE seems to have a problem receiving a whole HTTP response in a
  38.      single TLS record, so HTML pages containing \'\\r\\n\\r\\n\' won\'t
  39.      be displayed on IE.
  40.  
  41.     Add the following text into \'start_medusa.py\', in the \'HTTP Server\'
  42.     section::
  43.  
  44.         from tlslite.api import *
  45.         s = open("./serverX509Cert.pem").read()
  46.         x509 = X509()
  47.         x509.parse(s)
  48.         certChain = X509CertChain([x509])
  49.  
  50.         s = open("./serverX509Key.pem").read()
  51.         privateKey = parsePEMKey(s, private=True)
  52.  
  53.         class http_tls_channel(TLSAsyncDispatcherMixIn,
  54.                                http_server.http_channel):
  55.             ac_in_buffer_size = 16384
  56.  
  57.             def __init__ (self, server, conn, addr):
  58.                 http_server.http_channel.__init__(self, server, conn, addr)
  59.                 TLSAsyncDispatcherMixIn.__init__(self, conn)
  60.                 self.tlsConnection.ignoreAbruptClose = True
  61.                 self.setServerHandshakeOp(certChain=certChain,
  62.                                           privateKey=privateKey)
  63.  
  64.         hs.channel_class = http_tls_channel
  65.  
  66.     If the TLS layer raises an exception, the exception will be caught
  67.     in asyncore.dispatcher, which will call close() on this class.  The
  68.     TLS layer always closes the TLS connection before raising an
  69.     exception, so the close operation will complete right away, causing
  70.     asyncore.dispatcher.close() to be called, which closes the socket
  71.     and removes this instance from the asyncore loop.
  72.  
  73.     '''
  74.     
  75.     def __init__(self, sock = None):
  76.         AsyncStateMachine.__init__(self)
  77.         if sock:
  78.             self.tlsConnection = TLSConnection(sock)
  79.         
  80.         for cl in self.__class__.__bases__:
  81.             if cl != TLSAsyncDispatcherMixIn and cl != AsyncStateMachine:
  82.                 self.siblingClass = cl
  83.                 break
  84.                 continue
  85.         else:
  86.             raise AssertionError()
  87.  
  88.     
  89.     def readable(self):
  90.         result = self.wantsReadEvent()
  91.         if result != None:
  92.             return result
  93.         return self.siblingClass.readable(self)
  94.  
  95.     
  96.     def writable(self):
  97.         result = self.wantsWriteEvent()
  98.         if result != None:
  99.             return result
  100.         return self.siblingClass.writable(self)
  101.  
  102.     
  103.     def handle_read(self):
  104.         self.inReadEvent()
  105.  
  106.     
  107.     def handle_write(self):
  108.         self.inWriteEvent()
  109.  
  110.     
  111.     def outConnectEvent(self):
  112.         self.siblingClass.handle_connect(self)
  113.  
  114.     
  115.     def outCloseEvent(self):
  116.         asyncore.dispatcher.close(self)
  117.  
  118.     
  119.     def outReadEvent(self, readBuffer):
  120.         self.readBuffer = readBuffer
  121.         self.siblingClass.handle_read(self)
  122.  
  123.     
  124.     def outWriteEvent(self):
  125.         self.siblingClass.handle_write(self)
  126.  
  127.     
  128.     def recv(self, bufferSize = 16384):
  129.         if bufferSize < 16384 or self.readBuffer == None:
  130.             raise AssertionError()
  131.         self.readBuffer == None
  132.         returnValue = self.readBuffer
  133.         self.readBuffer = None
  134.         return returnValue
  135.  
  136.     
  137.     def send(self, writeBuffer):
  138.         self.setWriteOp(writeBuffer)
  139.         return len(writeBuffer)
  140.  
  141.     
  142.     def close(self):
  143.         if hasattr(self, 'tlsConnection'):
  144.             self.setCloseOp()
  145.         else:
  146.             asyncore.dispatcher.close(self)
  147.  
  148.  
  149.